package com.gzsxy.esjy.request;

import java.util.HashMap;
import java.util.Map;

/**
 * Post提交方式
 * Comparable接口是java提供的一个进行排序比较规则。
 * @author zhangxiaolong
 * @date 2022/7/6 9:36
 */
public class ContentType implements Comparable<ContentType>{

	private String type ;
	
	public static final ContentType APPLICATION_JSON = new ContentType("application/json");
	
	public static final ContentType APPLICATION_X_WWW_FORM_URLENCODED = new ContentType("application/x-www-form-urlencoded");
	
	public static final ContentType MULTIPART_FORM_DATA = new ContentType("multipart/form-data");
	
	public static final ContentType TEXT_XML = new ContentType("text/xml");
	
	public static final ContentType TEXT_PLAIN = new ContentType("text/plain");
	
	 private static final Map<String, ContentType> METHOD_MAP =
	            new HashMap<String, ContentType>();

    @Override
	public String toString() {
		return this.type;
	}

	static {
        METHOD_MAP.put(APPLICATION_JSON.toString(), APPLICATION_JSON);
        METHOD_MAP.put(APPLICATION_X_WWW_FORM_URLENCODED.toString(), APPLICATION_X_WWW_FORM_URLENCODED);
        METHOD_MAP.put(MULTIPART_FORM_DATA.toString(), MULTIPART_FORM_DATA);
        METHOD_MAP.put(TEXT_XML.toString(), TEXT_XML);
        METHOD_MAP.put(TEXT_PLAIN.toString(), TEXT_PLAIN);
    }

	//定义比较规则
	@Override
	public int compareTo(ContentType o) {
		return this.type.compareTo(o.type);
	}
	
	private ContentType(String type) {
		this.type = type ;
	}
	
	public static ContentType valueOf(String name) {
        if (name == null) {
            throw new NullPointerException("name");
        }

        name = name.trim();
        if (name.isEmpty()) {
            throw new IllegalArgumentException("empty name");
        }

        ContentType result = METHOD_MAP.get(name);
        if (result != null) {
            return result;
        } else {
            return new ContentType(name);
        }
	}

}
